<?php
/**
 * Created by PhpStorm.
 * User: xpz
 * Date: 2023/8/18
 * Time: 13:51
 */

namespace app\api\command\spider;
use app\api\command\spider\SpiderBase;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use think\config;
use think\Db;
use QL\QueryList;

class Gushiju extends SpiderBase
{
    private $baseDomain;
    private $createHtmlRootDir;
    private $ql;
    protected function configure()
    {
        $this->setName('gushiju')              		//定义命令的名字
        ->setDescription('gushiju')      //定义命令的描述
        ->addArgument('method')                   //参数1
        ->addArgument('id');                    //文章id
    }

    //    php think gushiju spider
    protected function execute(Input $input, Output $output)
    {
        $this->baseDomain = "https://www.gushiju.net/";
        require EXTEND_PATH.'phpQuery/phpQuery.php';
        $this->ql = QueryList::getInstance();
        ini_set('max_execution_time', 600);//秒为单位，自己根据需要定义
        $method = $input->getArgument('method');
        $this->createHtmlRootDir = ROOT_PATH."public/";
        if($method == 'details'){
            $this->details();
        } else if($method == 'test'){
            $url = 'https://www.gushiju.net/shici/48923';
            $url = 'https://www.gushiju.net/shici/7571';
            $url = 'https://www.gushiju.net/shici/224380';
//            $this->shici($url);
//            $this->test($url);
//            $this->spider();
            $this->spidershici();
        } else if($method == 'spider'){
            $this->spider();
        } else if($method == 'update'){
            $this->update();
        } else if($method == 'buchong'){
            $this->buchong();
        } else if($method == 'title'){
            $this->title();
        } else if($method == 'update2'){
            $this->update2();
        }

    }

    private function spider(){
        for($i=18023;$i<127524;$i++){
            sleep(1);
            $url = "https://www.gushiju.net/ju/chaodai-%E4%B8%8D%E9%99%90/zishu-%E4%B8%8D%E9%99%90/p-".$i;
            echo $url."\r\n";
            $html = _get_url($url);
//            print_r($html);exit;
            $data = $this->ql->html($html)->find('.biaoti a')->attrs('href');
            if(!empty($data)){
                foreach($data as $k=>$v){
                    $url = 'https://www.gushiju.net'.$v;
                    $this->test($url);
                }
            }
//            $data->destruct();
        }

    }

    private function update(){
        $this->ticheck();
        //        //延时0 到400秒
        $randTime = rand(0,400);
        echo "需要延时：".$randTime."\r\n";
        sleep($randTime);
        $articleModel = Model('app\common\model\Shiju');
        // var_dump($articleModel);exit;
        $articleData = $articleModel->where(['poetry_id'=>0])->limit(5)->order('id desc')->select();
        if(!empty($articleData)){
            foreach($articleData as $k=>$v){
                $sql = "SELECT * FROM `xpz_sc_poetry` WHERE title='".$v['poetry_name']."' and author='".$v['poet_name']."'";
                echo $sql."\r\n";
                $shirenData = Db::query($sql);
                if(!empty($shirenData)){
                    $shirenData = $shirenData[0];
                    $articleModel->where(['id'=>$v['id']])->update(['poetry_id'=>$shirenData['id'],'poet_id'=>$shirenData['author_id'],'status'=>1]);
                    //生成缓存
                    \think\Console::call('cachemanage', ['createshiju',$v['id']]);
                    echo $articleModel->getLastSql()."\r\n";
                } else {
                    $articleModel->where(['id'=>$v['id']])->update(['poetry_id'=>-1]);
                    echo $articleModel->getLastSql()."\r\n";
                }
            }
        }
    }

    private function update2(){
        $this->ticheck();
        //        //延时0 到400秒
        $randTime = rand(0,400);
        echo "需要延时：".$randTime."\r\n";
        sleep($randTime);
        $articleModel = Model('app\common\model\Shiju');
        // var_dump($articleModel);exit;
//        $articleData = $articleModel->where(['poetry_id'=>0])->limit(5)->order('id desc')->select();
        $sql = "SELECT * FROM `xpz_shiju` WHERE `poetry_id` = -1 and status = 2 and poet_name is not null ORDER BY `xpz_shiju`.`id` asc limit 5";
        $articleData = Db::query($sql);
        if(!empty($articleData)){
            foreach($articleData as $k=>$v){
                $saveData = [];
                if(empty($v['poet_id']) && !empty($v['poet_name'])){
                    $poet_sql = "select * from xpz_sc_poet where name='".$v['poet_name']."'";
                    $poet_rs = Db::query($poet_sql);
                    if(!empty($poet_rs)){
                        $saveData['poet_id'] = $poet_rs[0]['id'];
                    }
                }
                if(!empty($v['poetry_name'])){
                    $poetry_name = $this->_getPoetryName($v['poetry_name']);
                    $sql = "SELECT * FROM `xpz_sc_poetry` WHERE title like '%".$poetry_name."%' and author='".$v['poet_name']."' and content like '%".$v['title']."%'";
                    echo $sql."\r\n";
                    $shiciData = Db::query($sql);
                    if(!empty($shiciData)){
                        $shiciData = $shiciData[0];
                        $saveData['poetry_id'] = $shiciData['id'];
                        $saveData['status'] = 1;
                        $saveData['rank'] = $v['id'];
//                        print_r($saveData);
                        $articleModel->where(['id'=>$v['id']])->update($saveData);
                        echo $articleModel->getLastSql()."\r\n";
                        //生成缓存
                        \think\Console::call('cachemanage', ['createshiju',$v['id']]);

                    } else {
                        $articleModel->where(['id'=>$v['id']])->update(['poetry_id'=>-2]);
                        echo $articleModel->getLastSql()."\r\n";
                    }
                }

            }
        }
    }

    private function _getPoetryName($poetryName){
        $return = $poetryName;
        $poetryNameArr = [];
        if (strpos($poetryName, "/") !== false) {
            $poetryNameArr = explode('/',$poetryName);
        } else if(strpos($poetryName, "（") !== false) {
            $poetryNameArr = explode('（',$poetryName);
        } else if(strpos($poetryName, "·") !== false) {
            $poetryNameArr = explode('·',$poetryName);
        }
//        var_dump($poetryNameArr);
        if(count($poetryNameArr) >=2){
            $return = $poetryNameArr[0];
        }
        return $return;
    }




    private function shuzu($chaodao){
            $return = [
                '先秦' => ['id'=>'2','name'=>'先秦'],
                '两汉' => ['id'=>'10','name'=>'汉朝'],
                '魏晋' => ['id'=>'6','name'=>'魏晋'],
                '南北朝' => ['id'=>'9','name'=>'南北朝'],
                '五代' => ['id'=>'30','name'=>'五代'],
                '金朝' => ['id'=>'16','name'=>'金'],
                '隋代' => ['id'=>'14','name'=>'隋朝'],
                '唐代' => ['id'=>'4','name'=>'唐朝'],
                '宋代' => ['id'=>'1','name'=>'宋朝'],
                '元代' => ['id'=>'11','name'=>'元朝'],
                '明代' => ['id'=>'8','name'=>'明朝'],
                '清代' => ['id'=>'5','name'=>'清朝'],
                '现代' => ['id'=>'7','name'=>'现代'],
                '当代' => ['id'=>'12','name'=>'当代'],
                '近代' => ['id'=>'12','name'=>'近代'],
                '未知' => ['id'=>'','name'=>''],
            ];
            if(isset($return[$chaodao])){
                return $return[$chaodao];
            }
            return '';


    }

    private function sx($num){
        $returnarr = ['一','二','三','四','五','六','七','八','九','十'];
//        echo $num;exit;
        if(isset($returnarr[$num]) && !empty($returnarr[$num])){
            return $returnarr[$num];
        }
        return "";
    }

    private function _checkSpider($url){
        $articleModel = Model('app\common\model\Shiju');
        // var_dump($articleModel);exit;
        $articleData = $articleModel->where(['url_md5'=>md5($url)])->find();
        if(empty($articleData)){
            return true;
        } else {
            return false;
        }
        // var_dump($articleData);
    }

    private function test($url){

//        $url = 'https://www.gushiju.net/ju/513733';
//        $url = "https://www.gushiju.net/ju/768543";
        $saveData = ['source_url'=>$url,'url_md5'=>md5($url)];
        if($this->_checkSpider($url)){
            $html = _get_url($url);
//            print_r($html);exit;
            $data = $this->ql->html($html)->find('.shici-answer strong')->texts();
//        print_r($data);
            //朝代
            if(isset($data[0])){
                $dynastyName = $data[0];
                $dynastyNameArr = $this->shuzu($dynastyName);
                if(!empty($dynastyNameArr)){
                    $saveData['dynasty_name'] = $dynastyNameArr['name'];
                    $saveData['dynasty_id'] = $dynastyNameArr['id'];
                }
            }
            //作者
            if(isset($data[1])){
                $saveData['poet_name'] = $data[1];
            }

            $shiju = $this->ql->html($html)->find('.chaodai-zuozhe a')->htmls();

            if(count($shiju) == 2){
                $dyArr = $this->shuzu($shiju[0]);
                $saveData['dynasty_id'] = $dyArr['id'];
                $saveData['dynasty_name'] = $dyArr['name'];
                $saveData['poet_name'] = $shiju[1];
            }



            //shiju
            $shiju = $this->ql->html($html)->find('.sub-title')->texts();
            $shiju = str_replace(['”出处','“','”全诗'],['','',''],$shiju[0]);
            if(!empty($shiju)){
                $saveData['title'] = $shiju;
            }

//        //诗词名称
            $title = $this->ql->html($html)->find('.shici-title')->text();
            if(!empty($title)){
                $title = str_replace(['《','》'],["",""],$title);
                $saveData['poetry_name'] = $title;
            }
//        print_r($saveData);exit;
            $tagArr = $this->ql->html($html)->find('.shici-fenlei a')->texts();
            $tag = '';
            if(!empty($tagArr)){
                foreach($tagArr as $k=>$v){
                    $tag .=$v.',';
                }
                $tag = trim($tag,',');
                $saveData['tag'] = $tag;
            }
//        echo $tag;exit;
            //押韵
            $yayuns = $this->ql->html($html)->find('.shici-answer')->htmls();
//            print_r($yayuns);
            $yayun = '';
            if(!empty($yayuns) && count($yayuns) == 2){
                $yayun = "<p>".str_replace(' class="shici-zhushi"','',$yayuns[1]);
                $yayun = str_replace('<br>',"</p><p>",$yayun);
                $yayun = str_replace('<span>',"</p><span>",$yayun);
            } else if(!empty($yayuns)){
                $yayun = "<p>".str_replace(' class="shici-zhushi"','',$yayuns[0]);
                $yayun = str_replace('<br>',"</p><p>",$yayun);
                $yayun = str_replace('<span>',"</p><span>",$yayun);
            }
//            echo $yayun;exit;
            $saveData['yayun'] = $yayun;

            //网友评论
            $wyplarr = $this->ql->html($html)->find('.shici-content')->htmls();
//            print_r($wyplarr);exit;
            $wypl = '';
            if(count($wyplarr) == 2 && !empty($wyplarr[1])){
                $wypl = $wyplarr[1];
                if($wypl == '<br>'){
                    $wypl = '';
                }
                if(!empty($wypl)){
                    $wypl = str_replace('<br>',"</p><p>",$wypl);
                    $wypl ="<p>".$wypl."</p>";
                }

            }
            $saveData['wypl'] = $wypl;
//        echo $wypl;exit;
//        print_r($saveData);exit;

            $ziliao = $this->ql->html($html)->find('.ziliao')->htmls();
            $contentArr = [];
            if(!empty($ziliao)){
                $temp = [];
                $searchTitle = '创作背景';
                foreach($ziliao as $k=>$v){
                    $h2 = $this->ql->html($v)->find('h2')->text();
//                echo $h2."\r\n";
                    if(strpos($h2,$searchTitle) !== false){
                        $v = str_replace('<br>',"</p><p>",$v);
                        $v = replaceSpace(preg_replace('/<h2(.*?)<\/h2>/si', "xiaopzi_h2_title", $v));
                        $temp[$searchTitle][] ="<div class=\"xpz\">".$v."</div>";
                        unset($ziliao[$k]);
                    }

                }
                if(!empty($temp)){
                    shuffle($temp[$searchTitle]);
                    $contentArr[] = $temp;
                }


                $temp = [];
                $searchTitle = '翻译及注释';
                foreach($ziliao as $k=>$v){
                    $h2 = $this->ql->html($v)->find('h2')->text();
                    if(strpos($h2,$searchTitle) !== false){
                        $v = str_replace('<br>',"</p><p>",$v);
                        $v = replaceSpace(preg_replace('/<h2(.*?)<\/h2>/si', "xiaopzi_h2_title", $v));
                        $temp[$searchTitle][] ="<div class=\"xpz\">".$v."</div>";
                        unset($ziliao[$k]);
                    }

                }
                if(!empty($temp)){
                    shuffle($temp[$searchTitle]);
                    $contentArr[] = $temp;
                }


                $temp = [];
                $searchTitle = '赏析';
                foreach($ziliao as $k=>$v){
                    $h2 = $this->ql->html($v)->find('h2')->text();
                    if(strpos($h2,$searchTitle) !== false){
                        $v = str_replace('<br>',"</p><p>",$v);
                        $v = replaceSpace(preg_replace('/<h2(.*?)<\/h2>/si', "xiaopzi_h2_title", $v));
//                    print_r($v);exit;
                        $temp[$searchTitle][] ="<div class=\"xpz\">".$v."</div>";
                        unset($ziliao[$k]);
                    }

                }
                if(!empty($temp)){
                    shuffle($temp[$searchTitle]);
                    $contentArr[] = $temp;
                }


                $temp = [];
                $searchTitle = '鉴赏';
                foreach($ziliao as $k=>$v){
                    $h2 = $this->ql->html($v)->find('h2')->text();
                    if(strpos($h2,$searchTitle) !== false){
                        $v = str_replace('<br>',"</p><p>",$v);
                        $v = replaceSpace(preg_replace('/<h2(.*?)<\/h2>/si', "xiaopzi_h2_title", $v));
//                    print_r($v);exit;
                        $temp[$searchTitle][] ="<div class=\"xpz\">".$v."</div>";
                        unset($ziliao[$k]);
                    }

                }
                if(!empty($temp)){
                    shuffle($temp[$searchTitle]);
                    $contentArr[] = $temp;
                }

                //其他
                if(!empty($ziliao)){
                    foreach($ziliao as $k=>$v){
                        $h2 = $this->ql->html($v)->find('h2')->text();

                        $v = str_replace('<br>',"</p><p>",$v);
//                    print_r($v);exit;
                        $contentArr[][$h2][] ="<div class=\"xpz\">".$v."</div>";
                    }
                }
            }

//        print_r($contentArr);
//        exit;
            $content = '';
            if(!empty($contentArr)){
                foreach($contentArr as $k=>$v){
                    if(!empty($v)){
                        foreach($v as $key=>$val){
                            if(!empty($val)){
                                foreach($val as $k1=>$v1){
                                    if(count($val) > 1){
                                        $content .=str_replace('xiaopzi_h2_title',"<h2>".$key.$this->sx($k1)."</h2>",$v1);
                                    } else {
                                        $content .=str_replace('xiaopzi_h2_title',"<h2>".$key."</h2>",$v1);
                                    }

                                }
                            }
                        }
                    }

                }
            }

            if(empty($content)){
                $contentArr = $this->ql->html($html)->find('.shici-ziliao')->htmls();
//                print_r($contentArr);exit;
                if(isset($contentArr[0])){
                    $content = $contentArr[0];
                    $content = str_replace(['<br>','shici-zhushi'],["</p><p>",'span'],$content);
                    $content = "<div class=\"xpz\"><h2>《".$title."》翻译、赏析和诗意</h2>".$content."</div>";
                }
//                print_r($content);exit;
            }
            $saveData['content'] = $content;
            $saveData['add_time'] = time();
//            print_r($saveData);exit;
            $this->insert($saveData);

            sleep(1);
        } else {
            echo $url."已抓取\r\n";
        }

//        echo $content;
//        print_r($content);
    }

    private function insert($data){
        $articleModel = Model('app\common\model\Shiju');
        $content = $data['content'];
        $wangyou = $data['wypl'];
        $yayun = $data['yayun'];
        unset($data['content']);
        unset($data['wypl']);
        unset($data['yayun']);
        $id = $articleModel->insertGetId($data);
        $contentArr = [
            'shiju_id'=>$id,
            'content'=>$content,
            'wangyou' => $wangyou,
            'yayun' => $yayun,
        ];
        Model('app\common\model\ShijuContent')->insertGetId($contentArr);
        echo date("Y-m-d H:i:s")."=>".$data['title']."入库成功！\r\n";
    }


    private function buchong(){
        $articleModel = Model('app\common\model\Shiju');

        $sql = "SELECT * FROM `xpz_shiju` WHERE `poetry_id` = -1 and status = 2 and poet_name is null ORDER BY `xpz_shiju`.`id` asc limit 2000";
//        echo $sql;exit;
        $rs = Db::query($sql);
        if(!empty($rs)){
            foreach($rs as $k=>$v){
                $html = _get_url($v['source_url']);
//            print_r($html);exit;
//                $data = $this->ql->html($html)->find('.shici-answer strong')->texts();
                $shiju = $this->ql->html($html)->find('.chaodai-zuozhe a')->htmls();
                $updateData = [];
                if(count($shiju) == 2){
                    $dyArr = $this->shuzu($shiju[0]);
                    $updateData['dynasty_id'] = $dyArr['id'];
                    $updateData['dynasty_name'] = $dyArr['name'];
                    $updateData['poet_name'] = $shiju[1];
                }
                if(!empty($updateData['poet_name'])){
                    $dySql = "select * from xpz_sc_poet where name='".$updateData['poet_name']."'";
                    $dyRs = Db::query($dySql);
//                    print_r($dyRs);exit;
                    if(!empty($dyRs)){
                        $updateData['poet_id'] = $dyRs[0]['id'];
                    }
                }
                if(!empty($updateData)){
                    $articleModel->where(['id'=>$v['id']])->update($updateData);
                    echo $articleModel->getLastSql()."\r\n";
                }


            }
        }

    }

    private function title(){
        $articleModel = Model('app\common\model\Shiju');
        $sql = "SELECT * FROM `xpz_shiju` WHERE title like '%”全诗%' limit 11000";
        $rs = Db::query($sql);
        if(!empty($rs)){
            foreach($rs as $k=>$v){
                echo $v['title'].'=='.$v['id']."\r\n";
                $title = str_replace('”全诗','',$v['title']);
//                echo $title."\r\n";
                $articleModel->where(['id'=>$v['id']])->update(['title'=>$title]);
                echo $articleModel->getLastSql()."\r\n";
            }
        }
    }

    private function spidershici(){
        for($i=13245;$i<13248;$i++){
//            sleep(1);
            $url = "https://www.gushiju.net/shici/chaodai-%E4%B8%8D%E9%99%90/p-".$i;
            echo $url."\r\n";
            $html = _get_url($url);
//            print_r($html);exit;
            $data = $this->ql->html($html)->find('.biaoti a')->attrs('href');
            if(!empty($data)){
                foreach($data as $k=>$v){
                    $url = 'https://www.gushiju.net'.$v;
                    echo $url."\r\n";
                    $this->shici($url);
                }
            }
//            $data->destruct();
        }

    }
    private function shici($url){

//        $url = 'https://www.gushiju.net/ju/513733';
//        $url = "https://www.gushiju.net/ju/768543";
        $saveData = ['source_url'=>$url];
        if($this->_checkSpiderShici($url)){
            $html = _get_url($url);
            $body = $this->ql->html($html)->find('.shici-text')->html();
            if(empty($body)){
//                echo "诗词的内容为空，停止抓取！\r\n";
//                exit;
            }
            $body = trim($body,'<br>');
            $body = str_replace('<br>',"</p><p>",$body);
            $body = "<p>".$body."</p>";
            $saveData['body'] = $body;
//            print_r($body);exit;
//            print_r($html);exit;
            $shiju = $this->ql->html($html)->find('.chaodai-zuozhe a')->htmls();
//            print_r($shiju);exit;
            if(count($shiju) == 2){
                if($shiju[0] == '未知'){
                    $saveData['dynasty_id'] = 0;
                    $saveData['dynasty_name'] = '未知';
                } else{
                    $dyArr = $this->shuzu($shiju[0]);
                    $saveData['dynasty_id'] = $dyArr['id'];
                    $saveData['dynasty_name'] = $dyArr['name'];
                }
//                print_r($saveData);exit;
                $saveData['author'] = $shiju[1];
                $dySql = "select * from xpz_sc_poet where name='".$shiju[1]."'";
                $dyRs = Db::query($dySql);
//                    print_r($dyRs);exit;
                if(!empty($dyRs)){
                    $saveData['author_id'] = $dyRs[0]['id'];
                }
            }
//            print_r($saveData);exit;
            if(empty($saveData['dynasty_name']) || empty($saveData['author'])){
                echo "朝代或者诗人名称为空，停止抓取！\r\n";
                exit;
            }

            //title
            $title = $this->ql->html($html)->find('h1')->text();
            $titleArr = explode('》',$title);
            $title = $titleArr[0];
            $title = str_replace("《","",$title);
            if(empty($title)){
                echo "诗词名称为空，停止抓取！\r\n";
                exit;
            }
            $saveData['title'] = $title;
//            print_r($saveData);exit;

//        print_r($saveData);exit;
            $tagArr = $this->ql->html($html)->find('.shici-fenlei a')->texts();
            $tag = '';
            if(!empty($tagArr)){
                foreach($tagArr as $k=>$v){
                    $tag .=$v.',';
                }
                $tag = trim($tag,',');
                $saveData['label'] = $tag;
            }
            $tag2 = '';
            $tagArr2 = $this->ql->html($html)->find('.items-links')->htmls();
            if(!empty($tagArr2)){
                $tagArr2 = $this->ql->html($tagArr2[0])->find('a')->texts();
            }
            if(!empty($tagArr2)){
                foreach($tagArr2 as $k=>$v){
                    $tag2 .=$v.',';
                }
                $tag2 = trim($tag2,',');
                $saveData['label2'] = $tag2;
            }
//            print_r($saveData);exit;

            $shici_ziliao = $this->ql->html($html)->find('.shici-ziliao')->htmls();
//            print_r($shici_ziliao);exit;
            if(isset($shici_ziliao[0])){
                $content = $shici_ziliao[0];
                $ziliao = $this->ql->html($content)->find('.ziliao')->htmls();
                $contentArr = [];
                if(!empty($ziliao)){
                    $temp = [];
                    $searchTitle = '创作背景';
                    foreach($ziliao as $k=>$v){
                        $h2 = $this->ql->html($v)->find('h2')->text();
//                echo $h2."\r\n";
                        if(strpos($h2,$searchTitle) !== false){
                            $v = str_replace('<br>',"</p><p>",$v);
                            $v = replaceSpace(preg_replace('/<h2(.*?)<\/h2>/si', "xiaopzi_h2_title", $v));
                            $temp[$searchTitle][] ="<div class=\"xpz\">".$v."</div>";
                            unset($ziliao[$k]);
                        }

                    }
                    if(!empty($temp)){
                        shuffle($temp[$searchTitle]);
                        $contentArr[] = $temp;
                    }


                    $temp = [];
                    $searchTitle = '翻译及注释';
                    foreach($ziliao as $k=>$v){
                        $h2 = $this->ql->html($v)->find('h2')->text();
                        if(strpos($h2,$searchTitle) !== false){
                            $v = str_replace('<br>',"</p><p>",$v);
                            $v = replaceSpace(preg_replace('/<h2(.*?)<\/h2>/si', "xiaopzi_h2_title", $v));
                            $temp[$searchTitle][] ="<div class=\"xpz\">".$v."</div>";
                            unset($ziliao[$k]);
                        }

                    }
                    if(!empty($temp)){
                        shuffle($temp[$searchTitle]);
                        $contentArr[] = $temp;
                    }


                    $temp = [];
                    $searchTitle = '赏析';
                    foreach($ziliao as $k=>$v){
                        $h2 = $this->ql->html($v)->find('h2')->text();
                        if(strpos($h2,$searchTitle) !== false){
                            $v = str_replace('<br>',"</p><p>",$v);
                            $v = replaceSpace(preg_replace('/<h2(.*?)<\/h2>/si', "xiaopzi_h2_title", $v));
//                    print_r($v);exit;
                            $temp[$searchTitle][] ="<div class=\"xpz\">".$v."</div>";
                            unset($ziliao[$k]);
                        }

                    }
                    if(!empty($temp)){
                        shuffle($temp[$searchTitle]);
                        $contentArr[] = $temp;
                    }


                    $temp = [];
                    $searchTitle = '鉴赏';
                    foreach($ziliao as $k=>$v){
                        $h2 = $this->ql->html($v)->find('h2')->text();
                        if(strpos($h2,$searchTitle) !== false){
                            $v = str_replace('<br>',"</p><p>",$v);
                            $v = replaceSpace(preg_replace('/<h2(.*?)<\/h2>/si', "xiaopzi_h2_title", $v));
//                    print_r($v);exit;
                            $temp[$searchTitle][] ="<div class=\"xpz\">".$v."</div>";
                            unset($ziliao[$k]);
                        }

                    }
                    if(!empty($temp)){
                        shuffle($temp[$searchTitle]);
                        $contentArr[] = $temp;
                    }

                    //其他
                    if(!empty($ziliao)){
                        foreach($ziliao as $k=>$v){
                            $h2 = $this->ql->html($v)->find('h2')->text();

                            $v = str_replace('<br>',"</p><p>",$v);
//                    print_r($v);exit;
                            $contentArr[][$h2][] ="<div class=\"xpz\">".$v."</div>";
                        }
                    }
                }
                $content = '';
                if(!empty($contentArr)){
                    foreach($contentArr as $k=>$v){
                        if(!empty($v)){
                            foreach($v as $key=>$val){
                                if(!empty($val)){
                                    foreach($val as $k1=>$v1){
                                        if(count($val) > 1){
                                            $content .=str_replace('xiaopzi_h2_title',"<h2>".$key.$this->sx($k1)."</h2>",$v1);
                                        } else {
                                            $content .=str_replace('xiaopzi_h2_title',"<h2>".$key."</h2>",$v1);
                                        }

                                    }
                                }
                            }
                        }

                    }
                } else {
                    $content = $shici_ziliao[0];
                    $content = str_replace(['<br>','shici-zhushi'],["</p><p>","cankao"],$content);
                    $content = '<div class="xpz"><h2>'.$saveData['title'].'翻译、赏析和诗意</h2>'.$content.'</div>';
//                    print_r($content);exit;
                }
                if(empty($content)){
                    echo "1内容为空！，停止抓取\r\n";exit;
                }

            } else {
                echo "2内容为空！，停止抓取\r\n";exit;
            }

            $saveData['content'] = $content;
            //拼音
            if(isset($shici_ziliao[1])){
                $pinyin  = $shici_ziliao[1];
                $pinyin = str_replace(' style="line-height:22px;font-size:14px;"',"",$pinyin);
                $saveData['pinyin'] = $pinyin;
            }

            //网友评论
            if(isset($shici_ziliao[2])){
                $wangyou  = $shici_ziliao[2];
                $saveData['wangyou'] = $wangyou;
            }

//            print_r($saveData);
//            exit;

            $saveData['add_time'] = time();

            $articleModel = Model('app\common\model\Shici');
            $id = $articleModel->insertGetId($saveData);
            if(!empty($id)){
                echo date("Y-m-d H:i:s")."=>".$id."=>".$saveData['title']."入库成功\r\n";

            }
            sleep(1);
        } else {
            echo $url."已抓取\r\n";
        }

//        echo $content;
//        print_r($content);
    }

    private function _checkSpiderShici($url){
        $articleModel = Model('app\common\model\Shici');
        // var_dump($articleModel);exit;
        $articleData = $articleModel->where(['source_url'=>$url])->find();
        if(empty($articleData)){
            return true;
        } else {
            return false;
        }
        // var_dump($articleData);
    }
}